package psn.zjk.exporter;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;

import java.awt.*;
import java.io.File;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;

/**
 * @author sdyt_zhangjunkai
 * @version 1.0
 * @CLASS_NAME ExporterUtils
 * @DESCRIPTION
 * @CREATE_TIME 2020/9/10 20:14
 */
public class ExporterUtils {
    private static final String TEMPLATE_PATTERN = "{0}_{1}";
    public static final String DEFAULT_LOCALE = Locale.SIMPLIFIED_CHINESE.getLanguage().toUpperCase();

    private ExporterUtils() {
    }

    public static File getTemplateFile(String template, String locale) {
        String format = MessageFormat.format(TEMPLATE_PATTERN, template, locale);
        File file = new File(format);
        if (!file.exists()) {
            file = new File(template);
        }
        return file;
    }

    //默认样式
    public static XSSFCellStyle createDefaultStyle(ExcelSheetBuilder builder) {
        CellStyleBuilder styleBuilder = builder.newCellStyleBuilder();
        XSSFCellStyle defaultStyle = styleBuilder.setAlignment(HorizontalAlignment.LEFT)
                .setVerticalAlignment(VerticalAlignment.CENTER)
                .addBorder().build();
        return defaultStyle;
    }

    //列头样式
    public static XSSFCellStyle createHeaderStyle(ExcelSheetBuilder builder) {
        CellStyleBuilder styleBuilder = builder.newCellStyleBuilder();
        XSSFFont font = styleBuilder.createFont();
        font.setFontHeightInPoints((short) 12);

        styleBuilder.setAlignment(HorizontalAlignment.LEFT)
                .setVerticalAlignment(VerticalAlignment.CENTER)
                .setBackGroundColor(Color.LIGHT_GRAY)
                .setFont(font)
                .addBorder();
        XSSFCellStyle headerStyle = styleBuilder.build();
        return headerStyle;
    }

    /** 拷贝属性，除了序号 */
    public static void copyTo(SheetCells fromCells, SheetCells toCells) {
        String shownHeader = fromCells.getShownHeader();
        XSSFCellStyle style = fromCells.getStyle();
        List<Object> cells = fromCells.getCells();
        String header = fromCells.getHeader();
        Object pojo = fromCells.getPojo();
        if (shownHeader != null) {
            toCells.setShownHeader(shownHeader);
        }
        if (style != null) {
            toCells.setStyle(style);
        }
        if (cells != null) {
            toCells.setCells(cells);
        }
        if (header != null) {
            toCells.setHeader(header);
        }
        if (pojo != null) {
            toCells.setPojo(pojo);
        }
    }
}
